package com.lcu.student.service.impl;

import com.lcu.Result.Result;
import com.lcu.student.entity.PageBean;
import com.lcu.student.entity.PassStudent;
import com.lcu.student.entity.StudentGrades;
import com.lcu.student.mapper.StudentGradesMapper;
import com.lcu.student.service.StudentGradesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentGradesServiceImpl implements StudentGradesService {

    @Autowired
    private StudentGradesMapper studentGradesMapper;

    /**
     * 分页查询
     *
     * @param currentPage 当前页码
     * @param pageSize    每页展示条数
     * @return
     */
    @Override
    public Result selectByPage(int currentPage, int pageSize) {
        //计算从第几个数据开始查
        int begin = (currentPage - 1) * pageSize;
        //每页几条
        int size = pageSize;

        List<StudentGrades> rows = studentGradesMapper.selectByPage(begin, size);

        //查询总记录数
        int totalCount = studentGradesMapper.selectTotalCount();

        //封装PageBean对象
        PageBean<StudentGrades> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        return Result.success(pageBean);

    }

    /**
     * 带条件的分页查询
     *
     * @param currentPage   当前页码
     * @param pageSize      每页展示条数
     * @param studentGrades 查询条件（学号，姓名）
     * @return
     */
    @Override
    public Result selectByPage2(int currentPage, int pageSize, StudentGrades studentGrades) {
        //计算从第几个数据开始查
        int begin = (currentPage - 1) * pageSize;
        //每页几条
        int size = pageSize;

        //处理模糊查询的条件
        String studentName = studentGrades.getStudentName();
        if (studentName != null && studentName.length() != 0) {
            studentGrades.setStudentName("%" + studentName + "%");
        }
        String studentNumber = studentGrades.getStudentNumber();
        if (studentNumber != null && studentNumber.length() != 0) {
            studentGrades.setStudentNumber("%" + studentNumber + "%");
        }

        List<StudentGrades> rows = studentGradesMapper.selectByPage2(begin, size, studentGrades);

        //查询总记录数
        int totalCount = studentGradesMapper.selectTotalCount2(studentGrades);

        //封装PageBean对象
        PageBean<StudentGrades> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        return Result.success(pageBean);
    }

    /**
     * 添加数据
     *
     * @param studentGrades
     * @return
     */
    @Override
    public Result addGrades(StudentGrades studentGrades) {
        if (studentGrades.getStudentName()==""||studentGrades.getStudentNumber()==""||studentGrades.getSubjectNumber()==""){
            return Result.error("姓名，学号,课程名称不能为空,请重新添加！");
        }else {
            studentGradesMapper.addGrades(studentGrades);
            return Result.success();
        }
    }

    /**
     * 查询学生数据
     *
     * @return
     */
    @Override
    public Result selectStudent() {
        List<StudentGrades> list = studentGradesMapper.selectStudent();
        return Result.success(list);
    }

    /**
     * 根据姓名查学号
     *
     * @param studentName
     * @return
     */
    @Override
    public Result selectStudentNumber(String studentName) {
        List<StudentGrades> list = studentGradesMapper.selectStudentNumber(studentName);
        return Result.success(list);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteById(int id) {
        int i = studentGradesMapper.deleteById(id);
        return Result.success("成功删除" + i + "条数据");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public Result deleteByIds(List<Integer> ids) {
        studentGradesMapper.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据id修改数据
     *
     * @param studentGrades
     * @return
     */
    @Override
    public Result updateById(StudentGrades studentGrades) {

        String subjectName = studentGrades.getSubjectName();
        String subjectNumber = studentGradesMapper.selectSubjectNumber(subjectName);
        studentGrades.setSubjectNumber(subjectNumber);

        studentGradesMapper.updateById(studentGrades);
        return Result.success();
    }

    /**
     * 查询所有科目
     *
     * @return
     */
    @Override
    public Result selectSubjects() {
        List<String> subjects = studentGradesMapper.selectSubjects();
        return Result.success(subjects);
    }

    /**
     * 根据科目查询平均分
     *
     * @return
     */
    @Override
    public Result selectScores() {
        List<String> list = studentGradesMapper.selectScores();
        return Result.success(list);
    }

    /**
     * 根据科目查找及格人数
     * @return
     */
    @Override
    public Result selectPass() {
        //创建集合，将查出的数据存入集合
        List<PassStudent> list=new ArrayList<>();

        //查询出科目
        List<String> ids = studentGradesMapper.selectSubjects();
        String[] str = new String[ids.size()];
        String[] str1 = new String[ids.size()];
        //将集合转为数组
        for (int i = 0; i < ids.size(); i++) {
            str[i] = ids.get(i);
        }
        //根据科目查询出及格人数，并存入数组中
        for (int i = 0; i < str.length; i++) {
            str1[i] = studentGradesMapper.selectPass(str[i]);
        }
        //将两个数组的数据存入集合
        for (int i = 0; i < str.length; i++) {
            PassStudent passStudent=new PassStudent();
            passStudent.setGradesSubjects(str[i]);
            passStudent.setNumber(str1[i]);
            list.add(passStudent);
        }

        return Result.success(list);
    }
}
